// pages/personalMessage/personalMessage.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgSrc: '',
    city:'',
    showHidden:false,
    showDay: false,
    start_time: "",
    showID:false,
    region:'',
    userName:'谢威',
    userNames:'',
    haoma:'未设置',
    haomas:'',
    sex: ['未设置', '男', '女'],
    index: 0,
    illness:false,
    illnessConcen: [{ con: '类风湿关节炎 ' }, { con: '强直性脊柱炎' }, { con: '中轴性脊柱关节炎' }, { con: '外周性脊柱关节炎' }, { con: '痛风' }, { con: '系统性红斑狼疮 ' }, { con: '银屑病(关节炎)' }, { con: '骨关节炎' }, { con: '皮肤炎/多肌炎' }, { con: '干燥综合征' }, { con: '混合型结缔组织病 ' }, { con: '硬皮病' }, { con: '幼年特发性关节炎' }, { con: '白塞病' }, { con: '纤维肌痛综合征' }, { con: '韦格纳肉芽肿  ' }, { con: '血管炎' }, { con: '瑞特综合征' }, { con: '未分化脊柱关节病 ' }, { con: '重叠综合征' }, { con: '未分化结缔组织病 ' }, { con: '系统性硬化症' }, { con: '抗磷脂综合征' }, { con: '复发性多软骨炎' }, { con: '结节性红斑' }, { con: '风湿性多肌痛 ' }, { con: '定粉样变性关节炎' }],
    illnessShow:"未设置",
    jiuzhen:["未设置","是","否"],
    indexs:0,
    showAdd:false,
    showAdd:false,
    addChecked:false,
    addValue:'',
    illnessxuanzes:'',
    contentHidden:false,
    diagnosis_time:''
  },
  // 所患疾病
  selectIllness:function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showHidden: true,
      illness: true,
      // focus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this),200)
  },
  finish:function (e) {
    if (this.data.addChecked) {
      this.setData({
        illnessxuanzes: this.data.illnessxuanzes,
        addChecked: false
      })
    }else {
      this.setData({
        illnessxuanzes:''
      })
    }
    var that = this;
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    that.setData({
      animationData: animation.export(),
      userName: this.data.userNames,
      illnessShow: this.data.illnessxuanze + this.data.illnessxuanzes 
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showHidden: false,
        // focus: false,
        illness:false
      })
    }.bind(that), 200)
  },
  checkboxChange:function (e) {
    this.setData({
      illnessxuanze: e.detail.value 
    })
  },
  //手动添加疾病
  manualAdd:function (e) {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationDatas: animation.export(),
      showAdd: true,
      hiddenAdd: true,
      contentHidden:true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationDatas: animation.export(),

      })
    }.bind(this), 200)
  },
  //手动添加-隐藏
  hideAdd:function (e) {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationDatas: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationDatas: animation.export(),
        showAdd:false,
        hiddenAdd:false

      })
    }.bind(this), 200)
  },
  //手动添加-完成
  add_confirm: function (e) {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationDatas: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationDatas: animation.export(),
        showAdd: false,
        hiddenAdd: false,
        addChecked:true,
        illnessxuanzes: this.data.addValue
      })
    }.bind(this), 200)
  },
  getAdd:function (e) {
    this.setData({
      addValue: e.detail.value
    })
  },
  //性别
  bindsex:function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  //就诊情况
  doctor: function (e) {
    this.setData({
      indexs: e.detail.value
    })
  },
  //身份证号码
  selectID: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showHidden: true,
      showID: true,
      // focus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  gethaoma: function (e) {
    this.setData({
      haomas: e.detail.value
    })
  },
  // 身份证号码点击完成
  haoma_confirm: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      haoma: this.data.haomas
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false,
        showHidden: false,
        // focus: false,
        showID: false
      })
    }.bind(this), 200)
  },
  // 出生日期
  bindDateChange: function (e) {
    this.setData({
      start_time: e.detail.value
    })
  },
  // 确诊时间
  diagnoseTime: function (e) {
    this.setData({
      diagnosis_time: e.detail.value
    })
  },
  // 城市选择
  bindregion: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  //姓名
  selectDay: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showHidden: true,
      showDay: true,
      // focus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  getDur:function (e) {
    this.setData({
    userNames: e.detail.value
    })
  },
  // 姓名点击完成
  selet_confirm: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      userName: this.data.userNames
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false,
        showDay: false,
        showHidden: false,
        // focus: false,
        showID:false
      })
    }.bind(this), 200)
  },
  //姓名点击取消
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false,
        showDay: false,
        showHidden: false,
        // focus: false,
        showID:false,
        illness:false
      })
    }.bind(this), 200)
  },
  // 点击图片
  addImg: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0]
        if (tempFilePaths) {
          that.setData({
            hiddenImg: false
          })
        }
        that.setData({
          imgSrc: tempFilePaths
        });
        that.upload();
      }
    })
  },
  upload: function (e) {
    var that = this;
    var imgSrc = that.data.imgSrc;
    console.log(imgSrc)
    wx.uploadFile({
      url: app.d.ceshiUrl + '/Api/User/uploaduserimg',
      filePath: imgSrc,
      name: 'data',
      success: function (res) {
        console.log('uploadImage success, res is:', res);
        wx.request({
          url: app.d.ceshiUrl + '/Api/User/saveImg',
          data: {
            uid: app.d.userId,
            photo_x: res.data,
          },
          header: {// 设置请求的 header
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: function (res) {
            if (res.data.status == 1) {

            } else {
              wx.showToast({
                title: res.data.err,
              })
            }
          },
          fail: function () {
            // fail
            wx.showToast({
              title: '网络异常！',
              duration: 30000
            });
          },
        });
      },

      fail: function ({ errMsg }) {
        console.log('uploadImage fail, errMsg is', errMsg)
        return false;
      }
    })
  },
  update:function (e) {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/User/saveuserinfo',
      data: {
        uid: app.d.userId,
        uname:that.data.userName,
        birthday: that.data.start_time,
        id_cart: that.data.haoma,
        city: that.data.region,
        sex: that.data.sex[that.data.index],
        jibing: that.data.illnessShow,
        jiu_zhen: that.data.jiuzhen[that.data.indexs],
        que_zhen: that.data.diagnosis_time,
      },
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (res) {
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.err,
            duration:2000
          });
          setTimeout(function (){
            wx.navigateBack({
              
            })
          },2000);
        } else {
          wx.showToast({
            title: res.data.err,
          })
        }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 30000
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/User/getuserinfo',
      data: {
        uid: app.d.userId,
      },
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (res) {
        if (res.data.status == 1) {
          var info = res.data.info;
         that.setData({
          userName:info.uname,
          start_time:info.birthday,
          haoma:info.id_cart,
          region:info.city,
          index:info.sex,
          illnessShow:info.jibing,
          indexs:info.jiu_zhen,
          diagnosis_time:info.que_zhen,
          imgSrc:info.photo_x
         });
        } else {
          wx.showToast({
            title: res.data.err,
          })
        }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 30000
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})